package com.jiutian.cmsskitdev.service.Impl;

import com.jiutian.cmsskitdev.controller.DeviceManageController;
import com.jiutian.cmsskitdev.service.LoginService;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author: Jerryzaj
 * @description TODO
 * @date: 2024/5/27 15:34
 */

public class LoginServiceImpl implements LoginService {

    private static final Logger logger = Logger.getLogger(DeviceManageController.class.getName());


    @Override
    public Boolean login(String username, String password) throws IOException {
        if (validLogin(username, password)) {
            //openWindow();
            
        } else {
            logger.info("账号或密码错误，请重新登录");
        }
        return validLogin(username, password);
    }

    @Override
    public void sign() {

    }


    private void openHomePage() {
        try {
            Stage primaryStage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
            primaryStage.setTitle("首页");
            primaryStage.setScene(new Scene(root, 800, 600));
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void openWindow() throws IOException {

        VBox vBox = new VBox();
        //Scene scene = new Scene(vBox, 600, 600);
        Stage newWindow = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/com/jiutian/cmsskitdev/DeviceManage.fxml"));
        newWindow.setTitle("首页");

        newWindow.initModality(Modality.APPLICATION_MODAL); // 设置此模式，则窗口打开时，其他窗口无法操作
        newWindow.setScene(new Scene(root));

        newWindow.show();
    }

    public void loadAndShowMainView(Stage  stage) {
        try {
            // 加载MainView.fxml
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/example/DeviceManage.fxml"));
            Parent root = loader.load();
            Scene scene = new Scene(root);
            stage.setScene(scene); // 切换Scene
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Boolean validLogin(String username, String password){
        boolean isValid = "123456".equals(username) && "123456".equals(password);
        return isValid;
    }


}
